package com.dayaokeji.rhythmschoolstudent.wiget;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDialogFragment;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dayaokeji.rhythmschoolstudent.R;

/**
 * ********************************************************************
 * *  Created by Zeno on 2018/7/4
 * ********************************************************************
 * *  version 1.0
 * ********************************************************************
 * *  Copyright (C) dayaokeji.com 2018 -- 2020 All rights reserved.
 * ********************************************************************
 * * 单行输入dialog
 * ********************************************************************
 */
public class SingleInputDialog extends AppCompatDialogFragment implements View.OnClickListener {

    private static final String EXTRA_TITLE = "title";
    private static final String EXTRA_HIT = "hit";
    private static final String EXTRA_BTN_NAME = "btn_name";

    private String title;
    private String hit;
    private String btnName;

    private TextView tvDialogTitle;
    private EditText etHit;
    private Button btnSubmit;
    private ImageView ivCloseDialog;
    private OnClickListener onClickListener;
    private int inputType = InputType.TYPE_CLASS_TEXT;

    /**
     * 设置title and hit instance
     * @param title 标题
     * @param hit 输入框提示内容
     * @return  {@link SingleInputDialog} instance
     */
    public static SingleInputDialog newInstance(String title,String hit) {

        Bundle args = new Bundle();
        args.putString(EXTRA_TITLE, title);
        args.putString(EXTRA_HIT, hit);
        SingleInputDialog fragment = new SingleInputDialog();
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * 设置 title hit button
     * @param title 标题
     * @param hit 输入框提示内容
     * @param btnName 按钮名称
     * @return {@link SingleInputDialog} instance
     */
    public static SingleInputDialog newInstance(String title,String hit,String btnName) {

        Bundle args = new Bundle();
        args.putString(EXTRA_TITLE, title);
        args.putString(EXTRA_HIT, hit);
        args.putString(EXTRA_BTN_NAME, btnName);
        SingleInputDialog fragment = new SingleInputDialog();
        fragment.setArguments(args);
        return fragment;
    }
    
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (getDialog().getWindow() != null) {
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        setStyle(STYLE_NO_TITLE, 0);
        View rootView = inflater.inflate(R.layout.dialog_single_input, container, false);
        tvDialogTitle = rootView.findViewById(R.id.tv_dialog_title);
        etHit = rootView.findViewById(R.id.et_hit);
        etHit.setInputType(inputType);
        btnSubmit = rootView.findViewById(R.id.btn_submit);
        ivCloseDialog = rootView.findViewById(R.id.iv_close_dialog);
        btnSubmit.setOnClickListener(this);
        ivCloseDialog.setOnClickListener(this);
        return rootView;
    }

    public void setInputType(InType inType) {
        switch (inType) {
            case NUMBER:
                inputType = InputType.TYPE_CLASS_NUMBER;
                break;
            case STRING:
                inputType = InputType.TYPE_CLASS_TEXT;
                break;
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getArguments() != null) {
            this.title = getArguments().getString(EXTRA_TITLE);
            this.hit = getArguments().getString(EXTRA_HIT);
            this.btnName = getArguments().getString(EXTRA_BTN_NAME);
        }

        /*
          设置名称 ， 没有则设置展现默认的
         */
        if (!TextUtils.isEmpty(btnName)) {
            btnSubmit.setText(btnName);
        }
        if (!TextUtils.isEmpty(title)) {
            tvDialogTitle.setText(title);
        }
        if (!TextUtils.isEmpty(hit)) {
            etHit.setHint(hit);
        }
    }


    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_submit:
                if (onClickListener != null) {
                    final String s = etHit.getText().toString().trim();
                    onClickListener.onClick(s);
                }
                break;
            case R.id.iv_close_dialog:
                dismiss();
                break;
        }
    }

    public interface OnClickListener{
        void onClick(String str);
    }

    public enum InType {
        NUMBER,
        STRING
    }
}
